package com.uday.kraterchat.chatclient.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.uday.kraterchat.chatclient.business.UserServiceDelegate;
	import com.uday.kraterchat.chatclient.control.SubscribeMsgEvent;
	import com.uday.kraterchat.chatclient.data.MessageItem;
	import com.uday.kraterchat.chatclient.data.MessageType;
	import com.uday.kraterchat.chatclient.data.UserInfoBaseItem;
	import com.uday.kraterchat.chatclient.model.ChatClientModel;
	import com.uday.kraterchat.chatclient.view.ChatWindow;
	
	import flash.display.DisplayObject;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	import mx.messaging.events.MessageEvent;
	import mx.rpc.IResponder;

	public class SubscribeMsgCommand implements IResponder, ICommand
	{
		private var objChatClientModel:ChatClientModel = ChatClientModel.getInstance();
		
		public function SubscribeMsgCommand()
		{
		}

		public function result(event:Object):void
		{
			var objMessageItem:MessageItem = (event as MessageEvent).message.body as MessageItem;
			
			if(objMessageItem.strMessageType == MessageType.FRIEND_LIST){
				objChatClientModel.lstFriends = objMessageItem.lstDataItems;
			} else if(objMessageItem.strMessageType == MessageType.CHAT_MESSAGE){
				var currChatWin:Object = objChatClientModel.currChatWin;
				var lstFriends:ArrayCollection = objChatClientModel.lstFriends;
				//Open the chat window if not open
				if(currChatWin[objMessageItem.lngFrmUserId.toString()] == null){
					var objUserInfo:UserInfoBaseItem = null;
					
					for(var index:int;index < lstFriends.length;index++){
						objUserInfo = lstFriends.getItemAt(index) as UserInfoBaseItem;
						
						if(objUserInfo.lngUserID == objMessageItem.lngFrmUserId){
							break;
						}
					}
					
					//If the sender is found on the firend list open the window.
					if(objUserInfo && (index != objChatClientModel.lstFriends.length)){
						var objChatWin:ChatWindow = PopUpManager.createPopUp(Application.application as DisplayObject,ChatWindow) as ChatWindow;
						objChatWin.objUserInfo = objUserInfo;
						currChatWin[objMessageItem.lngFrmUserId.toString()] = objChatWin;
						
						PopUpManager.addPopUp(objChatWin,Application.application as DisplayObject);
						PopUpManager.centerPopUp(objChatWin);
						(currChatWin[objMessageItem.lngFrmUserId.toString()] as ChatWindow).parseMessage(objMessageItem.lstDataItems.getItemAt(0).toString());
					}
				} else{
					(currChatWin[objMessageItem.lngFrmUserId.toString()] as ChatWindow).parseMessage(objMessageItem.lstDataItems.getItemAt(0).toString());
				}
				objChatClientModel.strRecvdMessages += objMessageItem.lstDataItems.getItemAt(0).toString();
			}
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Error getting friends list.");
		}
		
		public function execute(event:CairngormEvent):void
		{
			var objLoginUserDel:UserServiceDelegate = new UserServiceDelegate(this);
			objLoginUserDel.subsribeMessages((event as SubscribeMsgEvent).strDestination);
		}
		
	}
}